//
//  RemindDetailWebView.m
//  HonlivApp
//
//  Created by admin on 2017/6/26.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "RemindDetailWebView.h"

@interface RemindDetailWebView ()<WKNavigationDelegate>

@property (nonatomic,strong) WKWebView *webView;

@end

@implementation RemindDetailWebView

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type == NewsTypeRemind) {
        self.title = @"温馨提醒";
    }else{
        self.title = @"董事长推荐";
    }
    [self initWebView];
    [SVProgressHUD show];
}

-(void)initWebView{
    
//    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-64) configuration:[self setConfig]];
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-64)];

    _webView.backgroundColor = Main_BGCOLOR;
    _webView.scrollView.bounces = NO;
    _webView.navigationDelegate = self;
    [self.view addSubview:_webView];

    [_webView loadHTMLString:self.news.content baseURL:nil];
    
}

-(WKWebViewConfiguration *)setConfig{
    NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
    
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;

    return wkWebConfig;
}

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{

    [SVProgressHUD dismiss];
}

-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    [SVProgressHUD showErrorWithStatus:@"加载失败..."];
}

-(BOOL)navigationShouldPopOnBackButton{
    if ([self.webView canGoBack]){
        [self.webView goBack];
        return NO;
    }
    return YES;
}

@end
